Chart for WPF and Silverlight > Tutorials > Data Binding Tutorials > Bind to a Data Table Declaratively |
Note: This tutorial applies to WPF applications. |
This tutorial provides step-by-step instructions for binding the C1Chart to a dataset declaratively. The data shows the information as a simple bar chart with one y-axis that represents the names of the products and one x-axis that represents the unit's price for each product. The products' ten most expensive products are displayed in descending order. The Bar chart uses one series to draw the unit price. A legend is not used since there is only one series.
The chart uses data from the sample Access database, Nwind.mdb. This tutorial assumes that the database file Nwind.mdb is in the Documents\ComponentOne Samples\Common directory, and refer to it by filename instead of the full path name for the sake of brevity.
Completing this tutorial will produce a chart that looks like the following:
To bind C1Chart to a data table declaratively, complete the following steps:
XAML |
Copy Code
|
---|---|
Title="Window1" Height="300" Width="500" xmlns:c1chart="clr-namespace:C1.WPF.C1Chart;assembly=C1.WPF.C1Chart" Loaded="Window_Loaded"> <Grid> <c1chart:C1Chart Content="" Margin="10,10,10,18" Name="c1Chart1"> <c1chart:C1Chart.Data> <c1chart:ChartData> <c1chart:ChartData.ItemNames>P1 P2 P3 P4 P5</c1chart:ChartData.ItemNames> <c1chart:DataSeries Label="Series 1" Values="20 22 19 24 25" /> <c1chart:DataSeries Label="Series 2" Values="8 12 10 12 15" /> </c1chart:ChartData> </c1chart:C1Chart.Data> <c1chart:Legend DockPanel.Dock="Right" /> </c1chart:C1Chart> </Grid> |
XAML Copy Codexmlns:sys="clr-namespace:System;assembly=mscorlib"
XAML Copy Code <Grid> <c1chart:C1Chart Margin="0" Name="c1Chart1" ChartType="Bar" Height="185" VerticalAlignment="Top"> <c1chart:C1Chart.Data> <c1chart:ChartData> <c1chart:DataSeries Label="Series 1" Values="20 22 19 24 25" /> <c1chart:DataSeries Label="Series 2" Values="8 12 10 12 15" /> </c1chart:ChartData> </c1chart:C1Chart.Data> <c1chart:Legend DockPanel.Dock="Right" /> </c1chart:C1Chart> <TextBlock DockPanel.Dock="Top" Text="Ten Most Expensive Products" HorizontalAlignment="Center"/> </Grid>Your chart appears like the following:
XAML Copy Code <TextBlock DockPanel.Dock="Top" Text="Ten Most Expensive Products" HorizontalAlignment="Center"/>Your XAML markup should now appear like the following:
XAML Copy Code <Grid> <c1chart:C1Chart Margin="0" Name="c1Chart1" ChartType="Bar" Height="185" VerticalAlignment="Top"> <c1chart:C1Chart.Data> <c1chart:ChartData> <c1chart:DataSeries Label="Series 1" Values="20 22 19 24 25" /> <c1chart:DataSeries Label="Series 2" Values="8 12 10 12 15" /> </c1chart:ChartData> </c1chart:C1Chart.Data> <c1chart:Legend DockPanel.Dock="Right" /> </c1chart:C1Chart> <TextBlock DockPanel.Dock="Top" Text="Ten Most Expensive Products" HorizontalAlignment="Center"/> </Grid>
Visual Basic Copy Code//WPF Imports System.Data Imports System.Data.OleDb Imports C1.WPF.C1Chart
C# Copy Code //WPF using System.Data; using System.Data.OleDb; using C1.WPF.C1Chart;
Visual Basic Copy Code Private _dataSet As DataSet Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs) ' create connection and fill data set Dim mdbFile As String = "c:\Program Files\ComponentOne Studio.NET 2.0\Common\nwind.mdb" Dim connString As String = String.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}", mdbFile) Dim conn As New OleDbConnection(connString) Dim adapter As New OleDbDataAdapter("SELECT TOP 10 ProductName, UnitPrice" & Chr(13) & "" & Chr(10) & " FROM Products ORDER BY UnitPrice DESC;", conn) _dataSet = New DataSet() adapter.Fill(_dataSet, "Products") ' set source for chart data c1Chart1.Data.ItemsSource = _dataSet.Tables("Products").Rows End Sub
C# Copy Code DataSet _dataSet; private void Window_Loaded(object sender, RoutedEventArgs e) { // create connection and fill data set string mdbFile = @"c:\Program Files\ComponentOne Studio.NET 2.0\Common\nwind.mdb"; string connString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}", mdbFile); OleDbConnection conn = new OleDbConnection(connString); OleDbDataAdapter adapter = new OleDbDataAdapter(@"SELECT TOP 10 ProductName, UnitPrice FROM Products ORDER BY UnitPrice DESC;", conn); _dataSet = new DataSet(); adapter.Fill(_dataSet, "Products"); // set source for chart data c1Chart1.Data.ItemsSource = _dataSet.Tables["Products"].Rows; }
Note: Make sure the file path for the mdbFile matches up to where you have the nwind.mdb database project located on your machine.
XAML Copy Code <c1chart:ChartData.ItemNames>P1 P2 P3 P4 P5</c1chart:ChartData.ItemNames> <c1chart:DataSeries Label="Series 1" Values="20 22 19 24 25" /> <c1chart:DataSeries Label="Series 2" Values="8 12 10 12 15" />The C1Chart control now appears empty on the Window.
XAML Copy Code <c1chart:ChartData ItemNameBinding="{Binding Path=[ProductName]}"> <c1chart:DataSeries ValueBinding="{Binding Path=[UnitPrice]}"/> </c1chart:ChartData>Your XAML code for your project should look like the following:
XAML Copy Code <Window x:Class="Chart for WPF_QuickStart.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" Title="Window1" Height="300" Width="500" Loaded="Window_Loaded" xmlns:c1chart="clr-namespace:C1.WPF.C1Chart;assembly=C1.WPF.C1Chart"> <Grid> <c1chart:C1Chart Margin="0" Name="c1Chart1" ChartType="Bar"> <TextBlock DockPanel.Dock="Top" Text="Ten Most Expensive Products" HorizontalAlignment="Center"/> <c1chart:C1Chart.Data> <c1chart:ChartData ItemNameBinding="{Binding Path=[ProductName]}"> <c1chart:DataSeries ValueBinding="{Binding Path=[UnitPrice]}"/> </c1chart:ChartData> </c1chart:C1Chart.Data> </c1chart:C1Chart> </Grid> </Window>
Observe the following at runtime
The chart is now populated with data from the Products table.